/**
 * 
 */
package com.fg.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.fg.dao.ProductDao;
import com.fg.model.ImageVO;
import com.fg.model.MemberVO;
import com.fg.model.ProductVO;
import com.fg.model.ReplyVO;

/**
 * @Package	: com.fg.action
 * @File 	: ProductAction.java
 *
 * @Author 	: JosephChoi
 * @Date	: 2011. 8. 13.
 * @Version : 
 * @Comment	:
 *
 */
public class ProductAction extends BaseAction {
	@Autowired
	private ProductDao productDao;
	
	
	/**
	 * @Comment	: 하나의 제품을 등록한다.
	 * @return resultMap(ProductVO객체를 product라는 키로 넣음)
	 * @throws Exception
	 */
	public String postProduct() throws Exception{
		
		String session_flag = null;
		if(parameters.containsKey("session_flag")){
			session_flag = parameters.get("session_flag");	//임시업로드된 이미지파일에 대한 정보
		}
		
	
		String type = parameters.get("regist_type");
		
		int regist_type;
		String p_point = null;
		if(type.equals("선물")){
			regist_type = 1;
		}else{
			regist_type = 2;
			if(parameters.containsKey("p_point")){
				p_point = parameters.get("p_point");
			}
		}
		String p_name = parameters.get("p_name");
		String p_detail = parameters.get("p_detail");
		
		MemberVO member = null;
		if(session.containsKey("memberVO")){
			member = (MemberVO)session.get("memberVO");
		}else{
			member = new MemberVO();
			member.setMember_seq(2);
		}
		
		
		Map data = new HashMap();
		data.put("p_name", p_name);
		data.put("p_detail", p_detail);
		data.put("regist_type", regist_type);
		data.put("seller", member.getMember_seq());
		if(p_point != null){
			data.put("p_point", p_point);
		}
		
		System.out.println(p_detail);
		int product_seq = productDao.insertProduct(data);
		if(product_seq < 0 ){
			return ERROR;
		}
		
		/******************* 이후 제품이미지 등록해야함.. ******************/
		List<ImageVO> image_list = new ArrayList<ImageVO>(); 
		if(session_flag != null){
			String[] flag = session_flag.split(",");
			for(int i=0 ; i<flag.length ; i++){
				if(flag[i].substring(0, 1).equals("f")){
					image_list.add((ImageVO)session.get(flag[i]));
				}
				session.remove(flag[i]);
			}
		}
		if(session_flag != null){
			for(int i=0; i<image_list.size() ; i++){
				image_list.get(i).setProduct_seq(product_seq);
				int product_image_seq = productDao.insertProductImage(image_list.get(i));
				if(product_image_seq < 0){
					return ERROR;
				}
			}
		}
		
		setInputStream("success");
		
		
		return SUCCESS;
	}
	
	
	
	/**
	 * @Comment	: 하나의 제품에 대한 상세설명을 호출한다.
	 * @return resultMap(ProductVO객체를 product라는 키로 넣음)
	 * @throws Exception
	 */
	public String loadProduct() throws Exception{
		int product_seq = Integer.parseInt(parameters.get("product_seq"));
		
		ProductVO product = productDao.readProductFromProductSeq(product_seq);
		
		List<ImageVO> image_list = productDao.readProductImageListFromProductSeq(product_seq);
		
		if(image_list.size() > 0){
			product.setImage_list(image_list);
		}
		
		if(product.getRegist_type().equals("선물")){
			List<ReplyVO> reply_list = productDao.readReplyListFromProductSeq(product_seq);
			product.setReply_list(reply_list);
		}
		System.out.println(product);
		resultMap.put("product", product);
		resultMap.put("me", true);
		return SUCCESS;
		
	}
	
	/**
	 * @Comment	: "선물" 제품에 대한 댓글을 단다, comment, product_seq를 가져온다.
	 * @return ReplyVO객체를 json으로 보낸다.
	 * @throws Exception
	 */
	public String postReply() throws Exception{
		String comment = parameters.get("comment");
		int product_seq = Integer.parseInt(parameters.get("product_seq"));
		
		MemberVO member = null;
		if(session.containsKey("memberVO")){
			member = (MemberVO)session.get("memberVO");
		}else{
			member = new MemberVO();
			member.setMember_seq(3);
			member.setName("이지수");
		}
		
		Map data = new HashMap();
		data.put("comment", comment);
		data.put("product_seq", product_seq);
		data.put("member_seq", member.getMember_seq());
		
		int reply_seq = productDao.insertReply(data);
		
		ReplyVO reply = new ReplyVO();
		reply.setReply_seq(reply_seq);
		reply.setInit_date(new Date());
		reply.setProduct_seq(product_seq);
		reply.setMember_seq(member.getMember_seq());
		reply.setMember_name(member.getName());
		reply.setComment(comment);
		reply.setChecked(false);
		
		setInputStream(reply);
		
		return SUCCESS;
	}
	
	/**
	 * @Comment	: 선물을 받는 것에 대한 액션
	 * product_seq를 필요로 한다
	 * @return
	 * @throws Exception
	 */
	public String receiveProduct() throws Exception{
		MemberVO member = null;
		if(session.containsKey("memberVO")){
			member = (MemberVO)session.get("memberVO");
		}else{
			member = new MemberVO();
			member.setMember_seq(2);
			member.setName("Test");
		}
		
		int product_seq = Integer.parseInt(parameters.get("product_seq"));
		
		Map data = new HashMap();
		data.put("buyer", member.getMember_seq());
		data.put("product_seq", product_seq);
		data.put("product_status", 2);	//완료
		int count = productDao.updateProduct(data);
		
		if(count < 1){
			return ERROR;
		}
		
		ProductVO product = productDao.readProductFromProductSeq(product_seq);
		setInputStream(product.getSeller());
		
		return SUCCESS;
	}
	
	/**
	 * @Comment	: 제품등록자가 댓글을 보고 맘에드는 글을 단 유저에게 상품을 준다.
	 * @return
	 * @throws Exception
	 */
	public String giveProduct() throws Exception{
		int reply_seq = Integer.parseInt(parameters.get("reply_seq"));
		int member_seq = Integer.parseInt(parameters.get("member_seq"));
		int product_seq = Integer.parseInt(parameters.get("product_seq"));
		
		int count = productDao.checkReplyFromReplySeq(reply_seq);
		if(count < 1){
			return ERROR;
		}
		
		Map data = new HashMap();
		data.put("buyer", member_seq);
		data.put("product_seq", product_seq);
		data.put("product_status", 2);	//완료
		
		count = productDao.updateProduct(data);
		
		MemberVO member = productDao.readMemberFromMemberSeq(member_seq);
		
		if(count < 1){
			return ERROR;
		}
		setInputStream(member);
		
		return SUCCESS;
	}
	
	public String loadProductList() throws Exception{
		int start = 0;
		if(parameters.containsKey("start")){
			start = Integer.parseInt(parameters.get("start"));
		}
		int count = 8;
		if(parameters.containsKey("end")){
			count = Integer.parseInt(parameters.get("count"));
		}
			
		Map data = new HashMap();
		data.put("start", start);
		data.put("count", count);
		List<ProductVO> product_list = productDao.readProductList(data);
		
		if(product_list.size() > 0){
			for(ProductVO product : product_list){
				List<ImageVO> image_list = productDao.readProductImageListFromProductSeq(product.getProduct_seq());
				
				if(image_list.size() > 0){
					product.setImage_list(image_list);
				}
			}
		}
		resultMap.put("product_list", product_list);
		
		return SUCCESS;
	}
	

}
